<?php
//全局通用的ajax请求
define('IN_T',true);
require 'include/init.php';

$act = Common::sfilter($_REQUEST['act']);

//更新微信分享数
if($act=='share_wx'){
	$c_id = intval($_POST['c_id']);
	$Db->execSql("update ".$Base->table('company_profile')." set share_num=share_num+1 where c_id=$c_id");
	$res = array('status'=>1);
	echo $Json->encode($res);
}
//更新访问量
else if($act=='view_num'){
	$c_id = intval($_REQUEST['c_id']);
	$Db->execSql("update ".$Base->table('company_profile')." set view_num=view_num+1 where c_id=$c_id");
	$view_num = $Db->query("select view_num from ".$Base->table('company_profile')." where c_id=$c_id",'One');
	$res = array('status'=>1,'view_num'=>$view_num);
	echo $Json->encode($res);
}
//微信签到 
else if($act=='wx_signin'){
	$data = array(
		'c_id' => isset($_POST['c_id']),
		'openid' => $_COOKIE['wx']['openid'],
	    'head_img'=>$_COOKIE['wx']['head_img'],
		'nickname'=>$_COOKIE['wx']['nickname'],
	    'sex'=>$_COOKIE['wx']['sex']==0?'未知':($_COOKIE['wx']['sex']==1?'男':'女'),
	    'province'=>$_COOKIE['wx']['province'],
	    'city'=>$_COOKIE['wx']['city']
	);
	$res['status'] = 0;
	if(empty($_COOKIE['wx']['openid'])){
		$res['msg'] = '请先微信登录';
	}
	else if($_COOKIE['wx']['signin']==1){
		$res['msg'] = '24小时内只能签到1次哦';
	}
	else{
		$Db->insert($Base->table('company_signin'),$data);
		setcookie('wx[signin]',1,Common::gmtime()+60*60*24,"/");
		$res = array('status'=>1);
	}
	echo $Json->encode($res);
	exit;
}
//提取微信签到列表
else if($act=='wx_signin_list'){
	$c_id = intval($_POST['c_id']);
	$sql = "select head_img from ".$Base->table('company_signin')." where c_id=$c_id order by id desc limit 20";
	$res = $Db->query($sql);
	if(empty($res)) $res = array();
	echo $Json->encode(array('count'=>count($res),'res'=>$res));
	exit;
}
//点赞
else if($act=='wx_like'){
	$pressed = intval($_POST['pressed']);
	$c_id = intval($_POST['c_id']);
	if($pressed){
		if($_COOKIE['wx']['like']==1){
			echo $Json->encode(array('status'=>0,'msg'=>'24小时内只能赞1次'));
			exit;
		}
		if(!$_COOKIE['wx']['signin']){
			echo $Json->encode(array('status'=>0,'msg'=>'必须先签到才能赞哦'));
			exit;
		}
		$Db->execSql("update ".$Base->table('company_profile')." set `like`=`like`+1 where c_id=$c_id");
		setcookie('wx[like]',1,Common::gmtime()+60*60*24,"/");
	}
	$sql = "select `like` from ".$Base->table('company_profile')." where c_id=$c_id ";
	$count = $Db->query($sql,'One');	
	echo $Json->encode(array('status'=>1,'msg'=>$count));
	exit;
}
//踩一脚
else if($act=='wx_unlike'){
	$pressed = intval($_POST['pressed']);
	$c_id = intval($_POST['c_id']);
	if($pressed){
		if($_COOKIE['wx']['unlike']==1){
			echo $Json->encode(array('status'=>0,'msg'=>'24小时内只能踩1次'));
			exit;
		}
		if(!$_COOKIE['wx']['signin']){
			echo $Json->encode(array('status'=>0,'msg'=>'必须先签到才能踩哦'));
			exit;
		}
		$Db->execSql("update ".$Base->table('company_profile')." set unlike=unlike+1 where c_id=$c_id");
		setcookie('wx[unlike]',1,Common::gmtime()+60*60*24,"/");
	}
	$sql = "select `unlike` from ".$Base->table('company_profile')." where c_id=$c_id ";
	$count = $Db->query($sql,'One');	
	echo $Json->encode(array('status'=>1,'msg'=>$count));
	exit;
}
?>